
import SdkEvent from "../SdkEvent";
import { JsNativeBridge } from "./JsNativeBridge";
let CLASS_NAME = 'sdk/JSBridge'

let SHOW_BANNER = 'showBanner'
let HIDE_BANNER = 'hideBanner'
let LOAD_BANNER = 'loadBanner'

let SHOW_INSERT = 'showInsertAd'
let LOAD_INSERT = 'loadInsertAd'

let LOAD_REWARD = 'loadRewardAd'
let SHOW_REWARD = 'showRewardAd'


class NativeAd {
    protected data = null;
    protected loadListener = new SdkEvent()
    protected errorListener = new SdkEvent()
    constructor(data: any) {
        this.data = data
        this.loadListener = new SdkEvent()
        this.errorListener = new SdkEvent()
    }
    onLoad(func: Function) {
        this.loadListener.on(func)
    }
    offLoad(func: Function) {
        this.loadListener.off(func)
    }
    emitLoad(data) {
        this.loadListener.emit(data)
    }
    onError(func: Function) {
        this.errorListener.on(func)

    }
    offError(func: Function) {
        this.errorListener.off(func)
    }
    emitError(data) {
        this.errorListener.emit(data)
    }
    destroy() {
        this.loadListener = new SdkEvent()
        this.errorListener = new SdkEvent()
    }
}


class NativeInterstitialAd extends NativeAd {
    constructor(data: any) {
        super(data)
    }

    show() {
        JsNativeBridge.callStaticMethod(CLASS_NAME, SHOW_INSERT, '')
    }

    load() {
        JsNativeBridge.callStaticMethod(CLASS_NAME, LOAD_INSERT, JSON.stringify(this.data), { callback: this.loadCallback.bind(this) })
    }

    loadCallback(result: number) {
        if (result == 1) {
            this.emitLoad(null)
        } else {
            this.emitError('加载失败')
        }

    }

}

class NativeRewardedVideoAd extends NativeAd {
    protected closeListener = new SdkEvent()
    constructor(data) {
        super(data)

    }
    load() {
        JsNativeBridge.callStaticMethod(CLASS_NAME, LOAD_REWARD, JSON.stringify(this.data), { callback: this.loadCallback.bind(this) })
    }
    show() {
        JsNativeBridge.callStaticMethod(CLASS_NAME, SHOW_REWARD, '')
    }
    loadCallback(result) {
        if (result == 1) {
            this.emitLoad(null)
        } else {
            this.emitError('加载失败')
        }
    }
    rewardAdClose(result) {
        this.emitClose({ isEnded: result })
    }
    destroy() {
        this.closeListener = new SdkEvent()
        NativeAd.call(this)
    }
    onClose(func: Function) {
        this.closeListener.on(func)
    }
    offClose(func: Function) {
        this.closeListener.off(func)
    }
    emitClose(data) {
        this.closeListener.emit(data)
    }

}
class NativeBannerAd extends NativeAd {
    protected resizeListener = new SdkEvent()
    load() {
        JsNativeBridge.callStaticMethod(CLASS_NAME, LOAD_BANNER,
            JSON.stringify(this.data), { callback: this.loadCallback.bind(this) })
    }
    hide() {
        JsNativeBridge.callStaticMethod(CLASS_NAME, HIDE_BANNER, '')
    }
    show() {
        JsNativeBridge.callStaticMethod(CLASS_NAME, SHOW_BANNER, '')
    }
    destroy() {
        NativeAd.call(this)
        this.resizeListener = new SdkEvent()
    }
    loadCallback(result) {
        console.log('bannerAdCallback result ', result)
        if (result == 1) {
            this.emitLoad(null)
        } else {
            this.emitError('加载失败')
        }
    }
    bannerResize(size) {
        this.emitLoad(size)
    }
    onResize(func: Function) {
        this.resizeListener.on(func)
    }
    offResize(func: Function) {
        this.resizeListener.off(func)
    }
    emitResize(data) {
        this.resizeListener.emit(data)
    }
}

export class Native {
    protected rewardIns: NativeRewardedVideoAd;
    protected bannerIns: NativeBannerAd;
    protected insertIns: NativeInterstitialAd;
    getSystemInfoSync() {

    }
    getDataPath() {
        return ''
    }
    createBannerAd(data) {
        if (!this.bannerIns) {
            this.bannerIns = new NativeBannerAd(data)
        }
        return this.bannerIns;
    }
    createRewardedVideoAd(data) {
        if (!this.rewardIns) {

            this.rewardIns = new NativeRewardedVideoAd(data);
        }
        return this.rewardIns;
    }
    createInterstitialAd(data) {
        if (!this.insertIns) {
            this.insertIns = new NativeInterstitialAd(data)
        }
        return this.insertIns;
    }
}
